﻿using MyImdb.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace MyImdb.Common
{
    public static class ServiceHelper
    {
        /// <summary>
        /// The base search URL
        /// </summary>
        public static string BaseSearchUrl = "http://imdbapi.org/?title={0}&type=json&plot=simple&episode=1&limit=10&yg=0&mt=none&lang=en-US&offset=&aka=simple&release=simple&business=0&tech=0";
        
        /// <summary>
        /// The base imdb title URL
        /// </summary>
        public static string BaseImdbTitleUrl = "http://imdbapi.org/?id={0}&type=json&plot=full&episode=1&lang=en-US&aka=simple&release=simple&business=0&tech=0";
        
        /// <summary>
        /// Creates Request Url
        /// </summary>
        /// <param name="targetUrl">string</param>
        /// <returns>string</returns>
        public static string CreateSearchRequestUrl(string searchText)
        {
            var url = string.Format(BaseSearchUrl, searchText);
            return url;
        }

        /// <summary>
        /// Creates Imdb Item Request Url
        /// </summary>
        /// <param name="targetUrl">string</param>
        /// <returns>string</returns>
        public static string CreateImdbItemRequestUrl(string imdbTitle)
        {
            var url = string.Format(BaseImdbTitleUrl, imdbTitle);
            return url;
        }

        /// <summary>
        /// Gets the search request.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <param name="response">The response.</param>
        public static void GetSearchRequest(string uri, Action<ObservableCollection<ImdbItem>> response)
        {
            MakeGetCall(uri, (sender, args) =>
            {
                if (args.Error != null)
                {
                    response(null);
                }
                else
                {
                    if (!args.Result.Contains("\"error\""))
                    {
                        var resp = JsonConvert.DeserializeObject<ObservableCollection<ImdbItem>>(args.Result);
                        response(resp);
                    }
                    else
                    {
                        response(null);
                    }
                }
            });
        }

        /// <summary>
        /// Gets the imdb item details.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <param name="response">The response.</param>
        public static void GetImdbItemDetails(string uri, Action<ImdbItem> response)
        {
            MakeGetCall(uri, (sender, args) =>
            {
                if (args.Error != null)
                {
                    response(null);
                }
                else
                {
                    var resp = JsonConvert.DeserializeObject<ImdbItem>(args.Result);
                    response(resp);
                }
            });
        }

        /// <summary>
        /// Makes a GET Call
        /// </summary>
        /// <param name="url">GET url.</param>
        /// <param name="completed">DownloadStringCompletedEventHandler</param>
        private static void MakeGetCall(string url, DownloadStringCompletedEventHandler completed)
        {
            HttpGet(url, completed);
        }

        /// <summary>
        /// Invokes HTTP GET method using an url.
        /// </summary>
        /// <param name="url">The GET url.</param>
        /// <param name="completed">DownloadStringCompletedEventHandler</param>
        private static void HttpGet(string url, DownloadStringCompletedEventHandler completed)
        {
            var wc = new WebClient();
            string getUrl = url;
            wc.DownloadStringCompleted += completed;
            wc.DownloadStringAsync(new Uri(getUrl, UriKind.RelativeOrAbsolute));
        }
    }
}
